/*   **********************************************************************  **
 **   Copyright notice                                                       **
 **                                                                          **
 **   (c) 2009, Around(J2)ME				                                 **
 **   All rights reserved.                                                   **
 **                                                                          **
 **   This program and the accompanying materials are made available under   **
 **   the terms of the New BSD License which accompanies this    			 **
 **   distribution.											                 **
 **   A copy is found in the textfile LICENSE.txt							 **
 **                                                                          **
 **   This copyright notice MUST APPEAR in all copies of the file!           **
 **                                                                          **
 **   Main developers:                                                       **
 **     Juri Strumpflohner		http://blog.js-development.com	             **
 **		Matthias Braunhofer		http://matthias.jimdo.com                    **
 **                                                                          **
 **  **********************************************************************  */

package com.aroundj2me.view;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.ImageItem;
import javax.microedition.lcdui.Spacer;
import javax.microedition.lcdui.StringItem;

import com.aroundj2me.controller.ApplicationController;
import com.aroundj2me.controller.ScreenController;
import com.aroundj2me.model.Comment;
import com.aroundj2me.model.Place;
import com.aroundj2me.uicomponents.CommentUI;
import com.aroundj2me.utility.ImageLoader;
/**
 * View for displaying the content of a Place
 *
 */
public class PlaceViewer extends Form implements CommandListener {
	private ApplicationController controller;
	private Place place;
	
	//commands
	private Command edit;
	private Command addComment;
	
	public PlaceViewer(ApplicationController controller) {
		super("Place Viewer");
		this.controller = controller;
		createCommands();
		this.setCommandListener(this);
	}

	/**
	 * Creates the commands
	 *
	 */
	private void createCommands() {
		edit = new Command("edit", "edit place", Command.SCREEN, 0);
		addComment = new Command("comment", "add comment", Command.SCREEN, 0);
		this.addCommand(edit);
		this.addCommand(addComment);
	}
	
	/**
	 * loads the place-data into the UI components
	 * for displaying to the user
	 *
	 */
	public void displayPlace(){
		//remove all components
		this.deleteAll();
		ImageItem usrImg = new ImageItem(this.place.getUser(), ImageLoader.getImage(ImageLoader.USER), ImageItem.LAYOUT_RIGHT, null);
		StringItem name = new StringItem(this.place.getName(), "(created: " + place.getCreationDate() + ")");
		name.setLayout(StringItem.LAYOUT_LEFT | StringItem.LAYOUT_NEWLINE_AFTER);
		String body = new String(this.place.getAddress());
		
		this.append(usrImg);
		this.append(name);
		Spacer spacer = new Spacer(this.getWidth(), 1);
		append(spacer);
		this.append(body);
		
		if(place.getComments().size() != 0){
			StringItem comments = new StringItem("User comments:", null);
			this.append(comments);
			for(int i=0; i<place.getComments().size(); i++){
				Comment c = (Comment) place.getComments().elementAt(i);
				String creationDate = c.getCreationDate();
				CommentUI comment;
				if(creationDate != null)
					comment = new CommentUI(c.getUser() + " wrote (" + creationDate + ")", c.getContent(), ImageLoader.getImage(ImageLoader.USER_TEST), this.getWidth());
				else
					comment = new CommentUI(c.getUser() + " wrote", c.getContent(), ImageLoader.getImage(ImageLoader.USER_TEST), this.getWidth());
				comment.setLayout(CommentUI.LAYOUT_NEWLINE_AFTER);
				this.append(comment);
			}
		}
	}
	
	/**
	 * Action handler
	 */
	public void commandAction(Command c, Displayable arg1) {
		if(c.getCommandType() == Command.BACK){
			ScreenController.getInstance().last();
		}else if(c == edit){
			controller.editPlace(this.place);
		}else if(c == addComment){
			controller.showCommentView();
		}
	}
	
	/**
	 * Resets the object in order to reuse them without having the
	 * need to instantiate a new one
	 *
	 */
	public void reset(){
		this.deleteAll();
	}
	
	/*
	 * Getters and setters
	 */
	public Place getPlace() {
		return place;
	}

	public void setPlace(Place place) {
		this.place = place;
	}
}